<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 重试
        class MultiplicatorUnitFailure extends Error { }
        function primitiveMultiply(a, b) {
            if (Math.random() < 0.2) {
                return a * b;
            } else {
                throw new MultiplicatorUnitFailure("i");
            }
        }
        function reliableMultiply(a, b) {
            for (; ;) {
                try {
                    return primitiveMultiply(a, b);
                } catch (e) {
                    if (!(e instanceof MultiplicatorUnitFailure))
                        throw e;
                }
            }
        }
        console.log(reliableMultiply(8, 8));
        // → 64


        // 上锁的箱子
        const box = {
            locked: true,
            unlock() { this.locked = false; },
            lock() { this.locked = true; },
            _content: [],
            get content() {
                if (this.locked) throw new Error("Locked!");
                return this._content;
            }
        };
        function withBoxUnlocked(body) {
            let locked = box.locked;
            if (!locked) {
                return body();
            }

            box.unlock();
            try {
                return body();
            } finally {
                box.lock();
            }
        }
        withBoxUnlocked(function () {
            box.content.push("gold piece");
        });
        try {
            withBoxUnlocked(function () {
                throw new Error("Pirates on the horizon! Abort!");
            });
        } catch (e) {
            console.log("Error raised:", e);
        }
        console.log(box.locked);
        // → true
    </script>
</body>

</html>